
package cn.hz.horse;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

 

public class EzRuner  extends Thread{
 
	private LinkedBlockingQueue qu; 

	protected  LinkedBlockingQueue succQu;
	protected  LinkedBlockingQueue errorQu;
	
	private boolean isRunning=true;
	private EzHandle handle;
	private List<Object> list= new ArrayList<Object>();
	private int timeouts=0;
	@Override
	public void run(){
	   while (isRunning) {
           try {
        	   if(EzCfg.handleType==0){
        		   run1();
        	   }else{
        		   runList();
        	   }
           } catch (Exception e) {
               e.printStackTrace();    
           }
       }
   }

	private boolean run1(){
		Object o=null;
		try{
    	   o=qu.take();
		}catch(Exception e){
			e.printStackTrace();
		}    
		if(o!=null){
			return  handle.handle(o);
		}
        return false;
     
	}
	
	private boolean runList(){
		boolean b=false;
		if(list.size()>=EzCfg.handMax){
			b=putHandle();
		}
		 Object o=null;
		try{
		  o=qu.poll(EzCfg.handQuTimeOut, TimeUnit.MINUTES);
		}catch(Exception e){
			 
		}
		if(o==null){
			timeouts++;
		}else{
			timeouts=0;
			list.add(o);
		}
		if(timeouts>=2&&list.size()>(EzCfg.handMax/3)){
			b=putHandle();
			timeouts=0;
		}
		return b;
		
	}
	
	private boolean putHandle(){
		 List<Object> list2=new ArrayList<Object>();
		 list2.addAll(list);
		 list.clear();
		 return handle.handleList(list2);
	}

	public LinkedBlockingQueue getQu() {
		return qu;
	}

	public void setQu(LinkedBlockingQueue qu) {
		this.qu = qu;
	}

	public LinkedBlockingQueue getSuccQu() {
		return succQu;
	}

	public void setSuccQu(LinkedBlockingQueue succQu) {
		this.succQu = succQu;
	}

	public LinkedBlockingQueue getErrorQu() {
		return errorQu;
	}

	public void setErrorQu(LinkedBlockingQueue errorQu) {
		this.errorQu = errorQu;
	}

	public boolean isRunning() {
		return isRunning;
	}

	public void setRunning(boolean isRunning) {
		this.isRunning = isRunning;
	}

	public EzHandle getHandle() {
		return handle;
	}

	public void setHandle(EzHandle handle) {
		this.handle = handle;
	}

	public List<Object> getList() {
		return list;
	}

	public void setList(List<Object> list) {
		this.list = list;
	}

	public int getTimeouts() {
		return timeouts;
	}

	public void setTimeouts(int timeouts) {
		this.timeouts = timeouts;
	} 
	
	
	
	
}
